package com.lzj.myvideo.controller;
import com.lzj.myvideo.common.ServerResponse;
import com.lzj.myvideo.service.UserService;
import com.shortvideo.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    @RequestMapping("query")
    public ServerResponse query(@RequestParam("userId")String userId){
        if (StringUtils.isBlank(userId)){
            return ServerResponse.createByError("用户为空");
        }
        User user = userService.queryUser(userId);
        if (user == null){
            return ServerResponse.createByError("查询出错");
        }
        return ServerResponse.createBySuccess(user);
    }

    @RequestMapping("/faceUpload")
    public ServerResponse faceUpload(@RequestParam("userId")String userId, @RequestParam("files") MultipartFile[] files){
        if (StringUtils.isBlank(userId)){
            return ServerResponse.createByError("用户为空");
        }
        ServerResponse serverResponse = userService.userFaceUpload(userId, files);
        if (serverResponse.isSuccess() == false){
            return ServerResponse.createByError("上传失败");
        }
        return ServerResponse.createBySuccess(serverResponse.getMsg());


    }

    @RequestMapping("/backgroundImagUpload")
    public ServerResponse backgroundImag(@RequestParam("userId")String userId, @RequestParam("files") MultipartFile[] files){
        if (StringUtils.isBlank(userId)){
            return ServerResponse.createByError("用户为空");
        }

        ServerResponse serverResponse = userService.userBackgroundUpload(userId, files);
        if (serverResponse.isSuccess() == false){
            return ServerResponse.createByError("上传失败");
        }
        return ServerResponse.createBySuccess(serverResponse.getMsg());
    }

    /**
     * 查询用户是否关注过指定用户
     * @param userId
     * @param followId
     * @return
     */
    @RequestMapping("isFollow")
    public ServerResponse isLikeVideo(@RequestParam("userId") String userId,
                                      @RequestParam("followId")String followId){
        if (StringUtils.equals(userId,"undefined") || userId == null){
            return ServerResponse.createByError("未登录");
        }
        Boolean isFollow = userService.isFollow(userId,followId);

        return ServerResponse.createBySuccess(isFollow);
    }

    @RequestMapping("/follow")
    public ServerResponse follow(String userId,String followId){
        if (StringUtils.equals(userId,"undefined") || userId == null){
            return ServerResponse.createByError("未登录");
        }

        ServerResponse serverResponse = userService.follow(userId,followId);
        if (!serverResponse.isSuccess()){
            return ServerResponse.createByError("关注失败");
        }
        return ServerResponse.createBySuccess();
    }

    @RequestMapping("/unfollow")
    public ServerResponse unfollow(String userId,String followId){
        if (StringUtils.equals(userId,"undefined") || userId == null){
            return ServerResponse.createByError("未登录");
        }
        ServerResponse serverResponse = userService.unfollow(userId,followId);
        if (!serverResponse.isSuccess()){
            return ServerResponse.createByError("取消关注失败");
        }
        return ServerResponse.createBySuccess();
    }

    @RequestMapping("/exception")
    public ServerResponse exceptionTest(String userId){
        throw new RuntimeException("异常");
    }
}
